import requests
import re

session = requests.session()
headers = {
    'cookie': 'sessionid=pwin2ge9j7t9ttj5m15pjwar9m4sj9tc;',
    'user-agent': 'yuanrenxue.project',
}
session.headers.update(headers)


# 主要难点就是 yuanrenxue_cookie 的获取
def init_cookie():
    # 注意url没有api
    url = f'https://match.yuanrenxue.com/match/13'
    res = session.get(url)

    # 更新 yuanrenxue_cookie
    yuanrenxue_cookie = ''.join(re.findall(r"'(.*?)'", res.text))[:-7]
    print("yuanrenxue_cookie==>", yuanrenxue_cookie)

    headers['cookie'] = headers['cookie'] + yuanrenxue_cookie
    session.headers.update(headers)


def get_data(page):
    url = f'https://match.yuanrenxue.com/api/match/13?page={page}'
    res = session.get(url)
    print("==>", res.text)
    return res.json()


def main():
    value_list = []
    init_cookie()
    for page in range(1, 6):
        res = get_data(page)
        value_list += [v['value'] for v in res['data']]
    print(value_list)
    print(sum(value_list))


if __name__ == '__main__':
    main()
